package com.five.controller.backstage;

import com.alibaba.fastjson.JSON;
import com.five.dao.backstage.ChuKuDao;
import com.five.dao.backstage.RuKuDao;
import com.five.dao.impl.backstage.ChuKuDaoImpl;
import com.five.dao.impl.backstage.RuKuDaoImpl;
import com.five.entity.backstage.TChukudetail;
import com.five.util.LayTable;
import com.five.util.PrintJsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: jianglin
 * Date: 2020/11/30  15:35
 */
@RestController
@RequestMapping("/chuku")
public class ChuKuController {
    @Autowired
        private ChuKuDao getChuKu= new ChuKuDaoImpl();

    @RequestMapping("/addchuku")
    public int ruKuAll(HttpServletRequest req) {
        //接受form表单数据转化为map形式添加入库
        Map map =(Map) JSON.parse(req.getParameter("data"));
        List<Map> table =(List<Map>) JSON.parse(req.getParameter("tableData"));

        int i = getChuKu.addchuKu(map);
        int a=0;
        int b=0;
        for (Map map1 : table) {
            a=getChuKu.addChuK(map1,i);
            b=getChuKu.addChuKus(map1);
        }if (b>0){
            return a;
        }
        return 0;
    }

    @RequestMapping("/queryChuKu")
    public LayTable query(Integer page, Integer limit, TChukudetail tChukudetail){
        List<Map> map= getChuKu.queryChuKu(page,limit,tChukudetail);
       int count= getChuKu.count(tChukudetail);
        return new LayTable(count,map);
    }
        //通过查询出库id查询出
    @RequestMapping("/queryChuKuId")
    public LayTable queryId(Integer chukuid){
        List<Map> map =  getChuKu.queryChuId(chukuid);
        LayTable table = new LayTable(map.size(), map);
        return table;
    }

    @RequestMapping("/clean")
    public void clean(String roomcode,HttpServletResponse response) throws IOException {
        int i = getChuKu.clean(roomcode);
        String s = JSON.toJSONString(i);
        PrintWriter writer = response.getWriter();
        writer.print(i);
        writer.flush();
        writer.close();

    }


}